
/*

  Program: 
   
  - This program decomposes market-specific individual growth rates into a firm, 
  sector and aggregate component. 
  - The firm-specific component is further decomposed into a common and a market-specific shock.
   
        `1': input file
	`2': growth rate variable
	`3': output file 
	
*/

capture program drop dGLM_4_threesteps_growth_regs
program define dGLM_4_threesteps_growth_regs

set more off

*** STAGE 1: CONSTRUCTING IDIOSYNCRATIC, SECTOR AND MACRO-SPECIFIC SHOCKS ***

use `1', clear

* Summary statistics on destinations (or countries)
sum id_c
local min_id_c = r(min)
local max_id_c = r(max)

* Construct temporary destination-specific files
* Note: shocks are constructed by destination-year
foreach P of numlist `min_id_c'/`max_id_c'  {
	
	* Only keep those sales with destination `P'
	keep if id_c==`P'
	
	sum year if id_c==`P' & `2'!=.
	local min_year = r(min)
	local max_year = r(max)
	
	save temp_`P', replace
	
	* Construct idiosyncratic, sector and macro-specific shocks by:
	* - destination and year
	* using the "dGLM_2_growth_regs" .do file from step 2
	foreach X of numlist `min_year'/`max_year'  {
		dGLM_2_growth_regs `2' $sect `X' $refsect
		save temp`P'`X', replace
		use temp_`P', clear
	}
	
	* Append year-specific files together for each destination
	* - Obtain "growth_`P'_regs" file which are country-specific
	if `min_year'==`max_year'  {
	
		use temp`P'`min_year', clear
		sort country year
		save growth_`P'_regs, replace
		
	} 
	
	else  {
	
		use temp`P'`min_year', clear
		local min_year1 = `min_year'+1
		foreach X of numlist `min_year1'/`max_year'{
			append using temp`P'`X'
			erase temp`P'`X'.dta
		}
	
		sort country year
		save growth_`P'_regs, replace
		
	}
	
	erase temp`P'`min_year'.dta
	erase temp_`P'.dta
	
	use `1', clear
	
}

* Append country-specific files together
* - This leads to a "master" file containing all idiosyncratic, sector and macro-specific shocks
local min_id_c1 = `min_id_c'+1
use growth_`min_id_c'_regs, clear

foreach P of numlist `min_id_c1'/`max_id_c'{
	append using growth_`P'_regs
	erase growth_`P'_regs.dta
}

* Save output "master" file
save `3', replace
erase growth_`min_id_c'_regs.dta

*** STAGE 2: DECOMPOSING IDIOSYNCRATIC SHOCKS INTO FIRM-COMMON/FIRM-MARKET-SPECIFIC SHOCKS ***

 /***********************************************************************************
   
   APPROACH IN A NUTSHELL:
   
   - Extract common and destination specific components of the estimated firm-level 
   residuals that were estimated in the first-step regressions above
	- Firm-level idiosyncratic component contains destination-specific demand and firm-level supply shocks
	- Firm-common component proxies for firm-level supply shock
	- Firm-destination specific component proxies for destination-specific demand shock
                
   - This approach is done for firms that have at least 2 destination markets
     - This leads to more reliable identification of these components
   
 ************************************************************************************/

* Load "master" file containing all shocks
use `3', clear

* Count amount of unique destinations for each firm-year pair
capture egen id_c2 = group(country)
egen q = count(id_c2), by(firmid year)

* Perform additional firm-level decomposition only for those firm-year pairs that have:
* - at least two destination markets in a given year
* Decomposition of idiosyncratic part has two components:
* - "common": simple mean of firm-level idiosyncratic component across destinations in a given year
* - "destination": deviation from firm-level idiosyncratic component from "common" component
egen gr_idio_com = mean(gr_idio) if q>1, by(firmid year)
gen gr_idio_dest = gr_idio - gr_idio_com if q>1

* Create temporary file with firm-level idiosyncratic components only
keep firmid country iso* year w_fn gr_idio_com gr_idio_dest
sort firmid country year

save temp_idio, replace

* Merge "common" and "destination" specific firm-level idiosyncratic components into "master" file with all shocks
use `3', clear
sort firmid country year
merge firmid country year using temp_idio
tab _m
drop _m

sort firmid country year
save `3', replace

erase temp_idio.dta

* End of .do file
end
